package net.gazhi.delonix.rbac.entity;

import static net.gazhi.delonix.rbac.entity.DX_TABLE.dx_role;
import static net.gazhi.delonix.rbac.entity.DX_TABLE.dx_role_action;

import java.sql.Timestamp;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.TableGenerator;

import org.hibernate.annotations.SortNatural;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * 角色
 * 
 * @author Jeffrey Lin
 *
 */
@Entity(name = dx_role)
public class Role {

	@ManyToMany(fetch = FetchType.LAZY, targetEntity = Action.class)
	@JoinTable(name = dx_role_action, joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "action_id"))
	@SortNatural
	private Set<Action> actions;

	@Id
	@GeneratedValue(generator = dx_role)
	@TableGenerator(name = dx_role, pkColumnValue = dx_role, allocationSize = 1)
	private int id;

	@Column
	@NotEmpty(message = "角色名称不能为空！")
	private String name;

	@Column
	private String remark;

	@Column(name = "create_time", nullable = true)
	private Timestamp createTime;

	@Column(name = "update_time", nullable = true)
	private Timestamp updateTime;

	@Column(name = "created_by")
	private String createdBy;

	@Column(name = "updated_by")
	private String updatedBy;

	public Role() {
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Timestamp getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Timestamp createTime) {
		this.createTime = createTime;
	}

	public Timestamp getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Timestamp updateTime) {
		this.updateTime = updateTime;
	}

	public String getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(String createdBy) {
		this.createdBy = createdBy;
	}

	public String getUpdatedBy() {
		return updatedBy;
	}

	public void setUpdatedBy(String updatedBy) {
		this.updatedBy = updatedBy;
	}

	public Set<Action> getActions() {
		actions = actions == null ? new TreeSet<Action>() : actions;
		return actions;
	}

	public void setActions(Set<Action> actions) {
		this.actions = actions;
	}

	/**
	 * 获取功能权限统计描述
	 * 
	 * @return
	 */
	public Map<String, Map<String, Integer>> getActionDescription() {
		Map<String, Map<String, Integer>> desc = new LinkedHashMap<String, Map<String, Integer>>();
		for (Action act : this.getActions()) {
			String moduleName = act.getController().getModule().getName();
			String ctrlName = act.getController().getName();
			if (desc.get(moduleName) == null) {
				desc.put(moduleName, new LinkedHashMap<String, Integer>());
			}
			Integer qty = desc.get(moduleName).get(ctrlName);
			desc.get(moduleName).put(ctrlName, (qty == null ? 0 : qty) + 1);
		}
		return desc;
	}

	@Override
	public String toString() {
		return "[id=" + this.id + ",name=" + this.name + ",remark=" + this.remark + "]";
	}

}
